package com.ideal.manage.esms.repository.project;

import com.ideal.manage.esms.bean.project.EquipmentManage;
import com.ideal.manage.esms.bean.project.ProjectManage;
import com.ideal.manage.esms.repository.framework.BaseRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * Created By Dai On 2018/4/25
 * Remark: 工程配置 - 对象管理 - 设备管理 Repository
 */
public interface EquipmentManageRepository extends BaseRepository<EquipmentManage, String> {
    /**
     * 根据项目查找所有的设备
     *
     * @param id
     * @return
     */
    @Query("select e from EquipmentManage e where e.isValid=1 and e.project.id=?1 order by e.equipmentType, e.createTime")
    List<EquipmentManage> findByProjectId(String id);

    /**
     * 根据父元素查找所有的项目
     *
     * @param id
     * @return
     */
    @Query("select e from EquipmentManage e where e.isValid=1 and e.parent.id=?1")
    List<EquipmentManage> findByParentId(String id);

    /**
     * 根据项目id寻找电池包
     *
     * @param id
     * @return
     */
    @Query("select e from EquipmentManage e where e.isValid=1 and e.project.id=?1 and e.equipmentType=2")
    List<EquipmentManage> findPackageByEquipmentId(String id);

    /**
     * 根据项目查找所有的父元素
     *
     * @param project_id
     * @return
     */
    @Query("select e from EquipmentManage e where e.isValid=1 and e.project.id=?1 and e.equipmentType<3 order by e.equipmentType, e.createTime")
    List<EquipmentManage> findParentByProjectId(String project_id);

    /**
     * 查找电池模组是否有重复的名字
     *
     * @param group_name
     * @param packageE
     * @param projectManage
     * @return
     */
    @Query("select e from  EquipmentManage e where e.isValid=1 and e.name=?1 and e.parent=?2 and e.project=?3")
    EquipmentManage findGroupIfHasTheSameName(String group_name, EquipmentManage packageE, ProjectManage projectManage);

    /**
     * 根据id数组找到对应的设备
     *
     * @param ids
     * @return
     */
    @Query("select e from EquipmentManage e where e.isValid=1 and e.project.id in ?1")
    List<EquipmentManage> findByProjectIds(String[] ids);

    /**
     * 根据唯一标识找到对应的设备
     *
     * @param code
     * @return
     */
    @Query("select e from EquipmentManage e where e.isValid=1 and e.code=?1")
    EquipmentManage findByCode(String code);

    /**
     * 根据项目实体类的list找到对应的设备
     *
     * @param projectManageList
     * @return
     */
    @Query("select e from EquipmentManage e where e.isValid=1 and e.project in ?1")
    List<EquipmentManage> findByProjects(List<ProjectManage> projectManageList);

    /**
     * 根据项目实体 找到 所有的电池单体
     *
     * @param projectManage
     * @return
     */
    @Query("select e from EquipmentManage e where e.isValid=1 and e.project=?1 and e.equipmentType=4")
    List<EquipmentManage> findBatterysByProject(ProjectManage projectManage);

    /**
     * 根据 电池包 找到 所有的电池单体
     *
     * @param
     * @return
     */
    @Query("select e from EquipmentManage e where e.isValid=1 and e.parent in ?1 and e.equipmentType=4")
    List<EquipmentManage> findBatterysByGroup(List<EquipmentManage> groupList);

//    -------------------------------------------------------------------------------------------------------- Dai的方法 End

    @Query("select e.id from EquipmentManage e where e.isValid=1 and e.project.id=?1")
    List<String> findIdListByProjectId(String id);

    @Query("select e from EquipmentManage e where e.isValid=1 and e.id=?1")
    EquipmentManage findOneById(String id);

    @Query("select a from EquipmentManage a where a.isValid=1 and a.id=?1")
    EquipmentManage findByEquipmentId(String id);

    @Query("select a from EquipmentManage a where a.isValid=1")
    List<EquipmentManage> findAllEquipments();

    @Query("select e from EquipmentManage e where e.equipmentType<5 and e.isValid=1")
    List<EquipmentManage> findByTypeAndIsVaild();

    @Query("select e from EquipmentManage e where e.isValid=1")
    List<EquipmentManage> findByisValid();

    @Query("select e from EquipmentManage e where e.isValid=1 and e.project.id=?1 and e.equipmentType=?2")
    List<EquipmentManage> findEquipments(String id, String equipmentType);

    @Query("select e from EquipmentManage e where e.isValid=1 and e.name=?1")
    EquipmentManage findByName(String equipmentName);

    @Query("select e from EquipmentManage e where e.isValid=1 and e.project.id = ?1")
    List<EquipmentManage> findByProjects(String id);

    @Query("select e from EquipmentManage e where e.isValid=1 and e.parent.id=?1")
    List<EquipmentManage> findByAllBattery(String id);

    @Query("select e.code from EquipmentManage e where e.isValid = 1 and  e.equipmentType=?1 and e.project.ip=?2 ")
    List<String> findByGroupBattery(String equipmentType, String ip);

    @Query("select e from EquipmentManage e where e.isValid = 1 and e.project.ip in ?1")
    List<EquipmentManage> findAllEquipmentsCode(List<String> ips);
    @Query("select e.code from EquipmentManage e where e.isValid =1 and e.id=?1")
    String findByMacid(String id);
}
